/**
 * @author ciwyaitd
 * @author levy
 *    重构显示数字/菜单权限控制
 * @date   16/1/7
 * @description []
 */

import TipNum from "../components/TipNum.js";

(function (lf, r) {
    
    var RC = r.createClass,
        doAction = lf.bridgeJs.doAction,
        ajax = lf.utils.ajax;

    var BtnIcon = RC({
        getInitialState: function () {
            return {
                useState: false, //为false时,num不启作用.
                num: ''
            }
        },
        render: function () {
            var dt = this.props.data,
                count = this.props.count,
                num = this.state.useState ? this.state.num : this.setupTaskCount(dt.url, count),
                filterClass = '';

            if (!dt.url) {
                filterClass = ' filter-blur'
            }
            //生产禅道,列丰才能见
            if (dt.icon == 'zen' && lf.userInfo.oemCode != 'lf_property')
                filterClass += ' ele-hide';

            return (
                <div onTouchEnd={this.clickFn} className={"item" + filterClass}>
                    <div className="circle">
                        <a ref="icon" className={dt.bgclass + " " + dt.icon}>
                            <TipNum num={num}/>
                        </a>
                    </div>
                    <p>{dt.name}</p>
                </div>
            )
        },
        /**
         * 更新了工单的数字,则别的数字也要更新
         */
        componentWillReceiveProps() {
            var url = this.props.data.url;

            //不得已而为之,因为获取数据的接口不是统一一个
            this.getApprovalNum(url);

            this.getReportListNum(url);

            this.getNoticeNum(url);

            this.getDailyWorkNum(url);

            this.getConsultNum(url);
        },
        //报事处理
        getReportListNum(url) {
            var me = this;
            if (url && url.indexOf('report-list') > -1) {
                ajax({
                    data: {
                        page: 1,
                        pageSize: 1,
                        staffId: lf.userInfo.id
                    },
                    success(data) {
                        me.setNum(data.maxCount);
                    }
                }, lf.api.report.list);
            }
        },
        //通知
        getNoticeNum(url) {
            if (url && url.indexOf('notice') > -1) {
                ajax({
                    data: {
                        staffId: lf.userInfo.id,
                        deptId: lf.userInfo.departmentId,
                    }
                }, lf.api.worktable.num, (data) => {
                    let num = 0;

                    data.forEach(item => {
                        num += item.number;
                    });
                    this.setNum(num);
                });
            }
        },
        //日常任务
        getDailyWorkNum(url) {
            if (url && url.indexOf('daily-work') > -1) {
                ajax({
                    type: 'post',
                    data: {
                        staffId: lf.userInfo.id,
                    }
                }, lf.api.worktable.taskNum, (data) => {
                    let num = 0;
                    for (let i in data) {
                        num += data[i];
                    }
                    this.setNum(num);
                });
            }
        },
        //报事咨询
        getConsultNum(url) {
            if (url && url.indexOf('report-advisory') > -1) {
                ajax({
                    type: 'get',
                    data: {
                        staffId: lf.userInfo.id,
                    }
                }, '/api/work/event/getEventConsultAmount', (data) => {
                    let num = 0;
                    num = parseInt(data.notCheckAmount) + parseInt(data.notReplyAmount);
                    this.setNum(num);
                });
            }
        },
        /**
         * 审批数字
         */
        getApprovalNum(url) {
            if (url && url.indexOf('approval') > -1) {
                ajax({
                    data: {
                        staffId: lf.userInfo.id
                    }
                }, lf.api.workflow.getCount, (data) => {
                    this.setNum(data.workFlowWaitCount);
                });
            }
        },
        setNum(num) {
            this.setState({
                useState: true,
                num: num
            });
        },
        /**
         * 设置工单统一接口返回的数字
         * @param url
         * @param count
         * @returns {*}
         */
        setupTaskCount: function (url, count) {
            if (!url || !count) return;

            if (url.indexOf('task-processing') > -1)
                return count.waitDealCount;

            if (url.indexOf('rush') > -1)
                return count.grabCount;

            if (url.indexOf('sign-for') > -1)
                return count.waitSignCount;
        },
        clickFn: function () {
            var dt = this.props.data;
            switch (dt.url) {
                case 'sign-in-night.html':
                    //夜班签到界面
                    doAction(dt.url, dt.name, 'signNight');
                    break;
                case 'outside.html':
                    //外勤签到界面
                    doAction(dt.url, dt.name, 'signOutside');
                    break;
                case 'report.html':
                    //报事界面
                    doAction(dt.url, dt.name, 'report');
                    break;
                default:
                    //一般页面,
                    doAction(dt.url + `?menuId=${dt.id}`, dt.name);
                    break;
            }
        }
    });

    var IndexBody = RC({
        getInitialState: function () {
            return {
                items: [],
                taskCount: {}, //工单相关数据
                itemList: [], //工单相关数据
                isDone: ''
            };
        },
        componentWillMount: function () {
            var me = this;

            lf.bridgeJs.getUser(function (user) {
                lf.appJs.doLoading(true);
                me.getMenu(user);
                me.getTaskCount(user);
            });

            lf.appJs.refresh((user) => this.getTaskCount(user));
        },
        //页面图标渲染
        getMenu(user){
            var me = this;
            ajax({
                type: 'post',
                data: {
                    staffId: user.id,
                    sysMenuId: 0,
                }
            }, lf.api.menuList, function (data) {
                me.setState({
                    itemList: data,
                    isDone: true
                });
            });
        },
        //抢单/签收/工单列表头部
        getTaskCount: function (user) {
            var me = this;
            ajax({
                data: {
                    actor: user.id
                },
                success: function (resp) {
                    me.setState({
                        taskCount: resp.data
                    });
                    lf.appJs.doLoading(false);
                }
            }, lf.api.listHeader);
        },
        initIcon: function () {
            var item2DArray = lf.utils.to2DArray(this.state.itemList || [], 4);

            return item2DArray.map((array, i) => {
                return (
                    <div key={'array' + i} className="item-row">
                        {
                            array.map(item => {
                                return <BtnIcon key={item.id} data={item} count={this.state.taskCount}/>
                            })
                        }
                    </div>
                );
            });
        },
        render: function () {
            if (this.state.isDone) {
                if (!this.state.itemList.length) //数据为空
                    return <div style={{textAlign: 'center', paddingTop: '1rem'}}>请联系管理员授权</div>
                else
                    return (
                        <div className="icon-con">
                            {this.initIcon()}
                        </div>
                    )
            } else {
                return null
            }
        }
    });

    r.render(
        <IndexBody />,
        document.getElementById('root'), function () {

        }
    )

})(liefeng, React);


